package org.duang.kit;

import java.util.HashMap;
import java.util.Map;

public class BeanKit {
	
	private static final Map<Class<?>, Object> beanMap = new HashMap<Class<?>, Object>();
	
	/**
     * 获取 Bean Map
     */
    public static Map<Class<?>, Object> getBeanMap() {
        return beanMap;
    }

    /**
     * 获取 Bean 实例
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> cls) {
        if (!beanMap.containsKey(cls)) {
            throw new RuntimeException("无法根据类名获取实例！" + cls);
        }
        return (T) beanMap.get(cls);
    }
    
    /**
     * 获取 Bean 实例
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Object targetObj, Class<T> cls) {
        if (!beanMap.containsKey(cls)) {
            throw new RuntimeException(targetObj.getClass().getName() +" 无法根据类名获取实例: " + cls + " , 请检查是否后缀名是否正确！");
        }
        return (T) beanMap.get(cls);
    }

    /**
     * 设置 Bean 实例
     */
    public static void setBean(Class<?> cls, Object obj) {
        beanMap.put(cls, obj);
    }
}
